<?php
//exit("Exit!!");
set_time_limit(0);

//來源地 DB
$_SourceDBHost = "192.168.5.121";
$_SourceDBUser = "kacino";
$_SourceDBPass = "cyriac_0426";
$_SourceDBName = "betUnion168netUTF8";
if (! $ConnSource = @mysql_connect($_SourceDBHost, $_SourceDBUser, $_SourceDBPass))exit("連線錯誤：無法連線至『來源地』資料庫主機！");
if (! @mysql_select_db($_SourceDBName, $ConnSource))exit("連線錯誤：無法開啟『來源地』資料庫！");

//目的地 DB
$_DestinationDBHost = "192.168.5.114";
$_DestinationDBUser = "kacino";
$_DestinationDBPass = "cyriac_0426";
$_DestinationDBName = "betBoxUSWinusUTF8";
if (! $ConnDestination = @mysql_connect($_DestinationDBHost, $_DestinationDBUser, $_DestinationDBPass))exit("連線錯誤：無法連線至『目的地』資料庫主機！");
if (! @mysql_select_db($_DestinationDBName, $ConnDestination))exit("連線錯誤：無法開啟『目的地』資料庫！");


//$arrLottoID = array(1, 2, 3);
$arrLottoID = array(2, 3);
//$arrLottoID = array(1);
//$arrLottoID = array(3);
//$arrBeforeDate[1] = "2009-08-03";//date("Y-m-d");
$arrBeforeDate[2] = "2010-11-12";//date("Y-m-d");
$arrBeforeDate[3] = "2010-11-12";//date("Y-m-d");
//$arrGameNo[1] = 50;
$arrGameNo[2] = 365;
$arrGameNo[3] = 365;

foreach($arrLottoID as $intLID){
	$strDate = $arrBeforeDate[$intLID];
	$intGameNo = $arrGameNo[$intLID];

	$strSQL = "select a.*, b.NumberNormal, b.NumberSpecial, b.isPayOff from `bet_Gamble` a, `bet_GambleScore` b where a.LottoID = ".$intLID." and a.GameDate <= '".$strDate."' and a.isEnd = '1' and a.ID = b.GambleID order by a.ID DESC limit ".$intGameNo;
	if(!$RS = mysql_query($strSQL, $ConnSource)){
		print $strSQL."<br>\n";
		print mysql_error($ConnSource)."<br>\n";
		exit();
	}
	//exit($strSQL);
	while($Rows = mysql_fetch_object($RS)){
		$strSQL = "insert into `bet_Gamble`(`LottoID`, `SerialNo`, `OSerialNo`, `GameDate`, `GameTime`, `EndGameTime`, `EndGameTimeSN`, `isAuto`, `isEnd`, `PayupDate`) values(".$Rows -> LottoID.", '".$Rows -> SerialNo."', '".$Rows -> OSerialNo."', '".$Rows -> GameDate."', '".$Rows -> GameTime."', '".$Rows -> EndGameTime."', '".$Rows -> EndGameTimeSN."', '".$Rows -> isAuto."', '".$Rows -> isEnd."', '".$Rows -> PayupDate."')";

		if(!mysql_query($strSQL, $ConnDestination)){
			print $strSQL."<br>\n";
			print mysql_error($ConnDestination)."<br>\n";
			exit();
		}
		$intGID_Destination = mysql_insert_id($ConnDestination);

		$strSQL = "insert into `bet_GambleScore`(`GambleID`, `NumberNormal`, `NumberSpecial`, `isPayOff`, `AddDate`, `AddTime`) values(".$intGID_Destination.", '".$Rows -> NumberNormal."', '".$Rows -> NumberSpecial."', ".$Rows -> isPayOff.", CURRENT_DATE(), CURRENT_TIME())";

		if(!mysql_query($strSQL, $ConnDestination)){
			print $strSQL."<br>\n";
			print mysql_error($ConnDestination)."<br>\n";
			exit();
		}
	}
	print "Lotto: ".$intLID." => OK\n";
}
?>